package thread;



/*
死锁问题:
  两个线程都拥有了自己的一把锁,现在都想要获取对方的那把锁,让自己都拥有两个锁
  这样就会造成死锁,谁也拿不到两把锁,都僵持住了

 */

public class ThreadDemo12 {
    public static void main(String[] args) {
        Object locker1=new Object();
        Object locker2=new Object();

        Thread t1=new Thread(()->{
            synchronized(locker1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2){
                    System.out.println("第一个线程拿到两把锁");
                }
            }
        },"第一个线程");

        Thread t2=new Thread(()->{
            synchronized(locker2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker1){
                    System.out.println("第二个线程拿到两把锁");
                }

            }
        },"第二个线程");

        t1.start();
        t2.start();
    }
}
